package com.tadpole.music.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.tadpole.music.R;

import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.home.SchoolGradeAdapter;
import com.tadpole.music.adapter.screen.ScreenClassAdapter;
import com.tadpole.music.adapter.screen.ScreenCollegeAdapter;
import com.tadpole.music.adapter.screen.ScreenQuestionAdapter;
import com.tadpole.music.adapter.screen.ScreenRankAdapter;
import com.tadpole.music.bean.question.QuestionClassBean;
import com.tadpole.music.bean.question.QuestionRankBean;
import com.tadpole.music.bean.question.UniversityBean;
import com.tadpole.music.bean.study.QuestionBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionClassIView;
import com.tadpole.music.iView.question.QuestionRankIView;
import com.tadpole.music.iView.question.UniversityIView;
import com.tadpole.music.presenter.question.QuestionClassPresenter;
import com.tadpole.music.presenter.question.QuestionRankPresenter;
import com.tadpole.music.presenter.question.UniversityPresenter;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.view.activity.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created By 孙岩
 * on 2020/9/12
 */
public class ScreenDialog extends BaseDialog implements UniversityIView, QuestionRankIView, QuestionClassIView {
    private TextView tvSubmit;
    //学院
    private RecyclerView rvCollege;
    private ScreenCollegeAdapter screenCollegeAdapter;
    //题目
    private RecyclerView rvQuestion;
    private ScreenQuestionAdapter screenQuestionAdapter;
    //等级
    private RecyclerView rvRank;
    private ScreenRankAdapter screenRankAdapter;
    //题目类型
    private RecyclerView rvClass;
    private ScreenClassAdapter screenClassAdapter;
//    private int index = 0;
    //获取学院的presenter
    private UniversityPresenter universityPresenter;
    //选择题目的数据
    private List<QuestionBean> questionList = new ArrayList<>();

    //等级 presenter
    private QuestionRankPresenter questionRankPresenter;
    //分类presenter
    private QuestionClassPresenter questionClassPresenter;
    private IScreen iScreen;
    private Activity activity;

    //数据
    private int college_id;
    private int rank_id;
    private int class_id;
    private int q_type = 2;

    private String college_name;
    private String rank_name;
    private String class_name;
    private String q_type_name = "实战真题";

    public ScreenDialog(@NonNull Context context) {
        super(context);
    }

    public ScreenDialog(@NonNull Activity activity, int themeResId) {
        super(activity, themeResId);
        this.activity = activity;
        initView();
    }

    protected ScreenDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(getOwnerActivity().LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.screen_dialog,
                null);
        tvSubmit = view.findViewById(R.id.tvSubmit);
        rvCollege = view.findViewById(R.id.rvCollege);
        rvQuestion = view.findViewById(R.id.rvQuestion);
        rvRank = view.findViewById(R.id.rvRank);
        rvClass = view.findViewById(R.id.rvClass);

        setContentView(view);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);


        //获取学院
        universityPresenter = new UniversityPresenter();
        universityPresenter.attachView(this);
        universityPresenter.getUniversity();

        //获取选择题目
        questionList.add(new QuestionBean("实战真题", false));
        questionList.add(new QuestionBean("模拟训练", false));

        //获取等级
        questionRankPresenter = new QuestionRankPresenter();
        questionRankPresenter.attachView(this);

        //获取分类
        questionClassPresenter = new QuestionClassPresenter();
        questionClassPresenter.attachView(this);
        initListeners();
        initData();
    }

    //渲染数据
    private void initData() {
        //题目
        questionList.get(0).setSelect(true);
        GridLayoutManager layoutManager1 = new GridLayoutManager(activity, 3);
        rvQuestion.setLayoutManager(layoutManager1);
        //创建适配器
        screenQuestionAdapter = new ScreenQuestionAdapter(activity, questionList);
        //给RecyclerView设置适配器
        rvQuestion.setAdapter(screenQuestionAdapter);
        rvQuestion.setNestedScrollingEnabled(false);

        screenQuestionAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                for (int i = 0; i < questionList.size(); i++) {
                    if (position == i) {
                        questionList.get(i).setSelect(true);
                    } else {
                        questionList.get(i).setSelect(false);
                    }
                }
                screenQuestionAdapter.notifyDataSetChanged();

                if (questionList.get(position).getName().equals("实战真题")) {
                    q_type = 2;
                    q_type_name = "实战真题";
                } else if (questionList.get(position).getName().equals("模拟训练")) {
                    q_type = 1;
                    q_type_name = "模拟训练";
                }
            }
        });
    }

    //学院回调
    @Override
    public void showUniversity(final List<UniversityBean.DataBean> collegeList) {
        if (collegeList != null && collegeList.size() != 0) {
            college_id = collegeList.get(0).getId();
            college_name = collegeList.get(0).getName();
            questionRankPresenter.getQuestionRank(collegeList.get(0).getId());

            //学院
            collegeList.get(0).setSelect(true);
            GridLayoutManager layoutManager = new GridLayoutManager(activity, 2);
            rvCollege.setLayoutManager(layoutManager);
            //创建适配器
            screenCollegeAdapter = new ScreenCollegeAdapter(activity, collegeList);
            //给RecyclerView设置适配器
            rvCollege.setAdapter(screenCollegeAdapter);
            rvCollege.setNestedScrollingEnabled(false);

            screenCollegeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    for (int i = 0; i < collegeList.size(); i++) {
                        if (position == i) {
                            collegeList.get(i).setSelect(true);
                        } else {
                            collegeList.get(i).setSelect(false);
                        }
                    }
                    screenCollegeAdapter.notifyDataSetChanged();
                    questionRankPresenter.getQuestionRank(collegeList.get(position).getId());
                    college_id = collegeList.get(position).getId();
                    college_name = collegeList.get(position).getName();
                }
            });
        }
    }


    //等级回调
    @Override
    public void showQuestionRank(final List<QuestionRankBean.DataBean> rankList) {
        if (rankList != null && rankList.size() != 0) {

            rank_id = rankList.get(0).getId();
            rank_name = rankList.get(0).getName();
            questionClassPresenter.getQuestion_class(String.valueOf(rankList.get(0).getCollege_id()), String.valueOf(rankList.get(0).getId()));
            //等级
            rankList.get(0).setSelect(true);
            GridLayoutManager layoutManager2 = new GridLayoutManager(activity, 3);
            rvRank.setLayoutManager(layoutManager2);
            //创建适配器
            screenRankAdapter = new ScreenRankAdapter(activity, rankList);
            //给RecyclerView设置适配器
            rvRank.setAdapter(screenRankAdapter);
            rvRank.setNestedScrollingEnabled(false);

            screenRankAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    for (int i = 0; i < rankList.size(); i++) {
                        if (position == i) {
                            rankList.get(i).setSelect(true);
                        } else {
                            rankList.get(i).setSelect(false);
                        }
                    }
                    screenRankAdapter.notifyDataSetChanged();
                    questionClassPresenter.getQuestion_class(String.valueOf(rankList.get(position).getCollege_id()), String.valueOf(rankList.get(position).getId()));
                    rank_id = rankList.get(position).getId();
                    rank_name = rankList.get(position).getName();
                }
            });
        }
    }

    //分类回调
    @Override
    public void showQuestionClass(final List<QuestionClassBean.DataBean> classList) {
        if (classList != null && classList.size() != 0) {
            class_id = classList.get(0).getId();
            class_name = classList.get(0).getName();
            //题目类型
            classList.get(0).setSelect(true);
            GridLayoutManager layoutManager3 = new GridLayoutManager(activity, 3);
            rvClass.setLayoutManager(layoutManager3);
            //创建适配器
            screenClassAdapter = new ScreenClassAdapter(activity, classList);
            //给RecyclerView设置适配器
            rvClass.setAdapter(screenClassAdapter);
            rvClass.setNestedScrollingEnabled(false);

            screenClassAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    for (int i = 0; i < classList.size(); i++) {
                        if (position == i) {
                            classList.get(i).setSelect(true);
                        } else {
                            classList.get(i).setSelect(false);
                        }
                    }
                    screenClassAdapter.notifyDataSetChanged();
                    class_id = classList.get(position).getId();
                    class_name = classList.get(position).getName();
                }
            });
        }
    }

    @Override
    public void show401() {
        SpUtil.getInstance(activity).getString(Constant.TOKEN, "");
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    //初始化监听器
    private void initListeners() {
        //提交
        tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                iScreen.getScreenMode(1,college_id,college_name,rank_id,class_id,class_name,q_type);
            }
        });
    }

    public void setScreenListener(IScreen iScreen) {
        this.iScreen = iScreen;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showErr() {

    }

    public interface IScreen {
        void getScreenMode(int mode, int college_id,String college_name, int rank_id, int class_id,String class_name, int q_type);
    }
}
